from flask import Flask, render_template, url_for, request
from web.url import load_urls

def start(module):
    from urls import urls
    from cherrypy import wsgiserver
    import config

    # TODO: perform a LOT of validation here

    # changing the directories here is a bit dodgy, but yeah.
    app = Flask('nanaki', static_folder='../static', template_folder='../templates')
    load_urls(app, urls)

    host = config.HOST
    port = config.PORT
    app.debug = config.DEBUG

    if app.debug:
        app.run(host, port)
    else:
        d = wsgiserver.WSGIPathInfoDispatcher({'/': app})
        server = wsgiserver.CherryPyWSGIServer((host, port), d)

        print 'starting nanaki, serving %s...' % module
        try:
            print 'serving on http://%s:%s/' % (host, port)
            server.start()
        except KeyboardInterrupt:
            server.stop()
            print 'shutting down nanaki serving %s...' % module

def create(name):
    """directories to create:

        (name)
            - static/
            - templates/
            - urls.py
            - views.py
            - forms.py
            - (copy over manage.py)
            - __init__.py

    """
    cwd = os.getcwd()
    os.mkdir(name)
    os.chdir(name)
    os.chdir(cwd)
    raise NotImplementedError("You can't create projects via nanaki yet!")
